<?php
/**
 * @filename compression-class.php
 * @author LD King
 * @encoding UTF-8
 * @date 2008-9-17 | 下午08:06:51
 * @package wp-simple-backup
 * @subpackage 
 * @usage 压缩 类 [ gz, bz2, zip ]
 * @note 
 * @copyright Copyright (c) 2007 - 2008 LD (http://mifunny.info)
 * @version $Id: 0.1 $
 */

/**
 * 压缩 抽象类
 *
 */
abstract class compression {

	/**
	 * 备份目录
	 *
	 * @var string $backupPath 
	 */
	public $backupPath;
	
	/**
	 * 备份后的压缩文件
	 *
	 * @var string $backupFile
	 */
	public $backupFile;
	
	/**
	 * 使用相对路径代替主目录的正则表达式
	 */
	public $patternAbs;
	
	/**
	 * 初始化，备份文件的文件名
	 *
	 * @param string $backupPath
	 */
	public function __construct($backupPath) {
		$this->backupPath = $backupPath;
		$this->backupFile = $backupPath.'/wp-'.date('Ymd-His');
		//$this->patternAbs = '/'.preg_replace('/\//', '\/', ABSPATH).'/';
		$this->patternAbs = '/'.preg_replace('/\//', '\/', '/home/http/mifunny/').'/';
	}
	
	/**
	 * 初始化 需要压缩的文件路径列表，传入hook
	 *
	 * @param string $directory
	 * @param array $exclude
	 */
	public function compress($directory, $exclude) {
		
		/**
		 * 迭代方法得到目录文件
		 */
		$dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory), true);
		
		/**
		 * 得到排除目录的正则表达式
		 */
		$patternEx = '/';
		for ($i=0; $i < count($exclude) ; $i++) {
			$patternEx .= preg_replace('/\//', '\/', $exclude[$i]).'|';
		}
		$patternEx .= preg_replace('/\//', '\/', $this->backupPath).'/';

		/**
		 * 循环排除 $exclude
		 */
		foreach ($dir as $key => $value) {
			$key = preg_grep($patternEx, array($key));
			if (empty($key)) {
				$this->hook($value);
			}
		}/* END foreach */		
	
	} /* END func compress */
	
	/**
	 * 模板方法,提供各个子类重写
	 *
	 * @param string $filePath
	 */
	public abstract function hook($filePath);

} /* END abs class compression */



/**
 * .gz 格式
 */
class backupToGz extends compression {

	/**
	 * @see compression::hook()
	 *
	 * @param string $filePath
	 */
	public function hook($filePath) {
	}
} /* END class backupToGz */


/**
 * .bz2 格式
 */
class backupToBz2 extends compression {
	/**
	 * @see compression::hook()
	 *
	 * @param string $filePath
	 */
	public function hook($filePath) {
	}
} /* END class backupToBz2 */


/**
 * .zip 格式
 */
class backupToZip extends compression {
	
	/**
	 * zip 对象
	 *
	 * @var ZipArchive $zip
	 */
	public $zip;
	
	/**
	 * 初始化 zip 对象 
	 *
	 * @param string $backupPath
	 */
	public function __construct($backupPath) {
		parent::__construct($backupPath);
		$this->backupFile .= '.zip';
		
		$this->zip = new ZipArchive();
		$this->zip->open($this->backupFile, ZipArchive::OVERWRITE);
	}
	
	/**
	 * 关闭 zip 对象
	 *
	 */
	public function __destruct() {
		$this->zip->close();
	}
	
	/**
	 * @see compression::hook()
	 *
	 * @param string $filePath
	 */
	public function hook($filePath) {
		if (is_dir($filePath)) {
			$this->zip->addEmptyDir(preg_replace($this->patternAbs,'wordpress/', $filePath));
		}else {
			$this->zip->addFile($filePath, preg_replace($this->patternAbs,'wordpress/', $filePath));
		}
	} /* END func hook */

} /* END class backupToZip */
?>